home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / SCSI Samples 1.0 / SCSI Async Sample 06⁄15 ƒ / Src / StringFormat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-16  |  5.4 KB  |  298 lines  |  [TEXT/KAHL]

  1. /*                                    StringFormat.c                            */
  2. /*
  3.  * StringFormat.c
  4.  * Copyright © 1993 Apple Computer Inc. All rights reserved.
  5.  *
  6.  * These functions convert values to readable text.
  7.  */
  8. #include <Memory.h>
  9. #include <OSUtils.h>
  10. #include <stdio.h>
  11. #pragma segment LogManager
  12.  
  13. #define NUL        '\0'
  14.  
  15. /*
  16.  * AppendChar writes a character into the string. Note that
  17.  * it wraps around if the string size exceeds 255 bytes.
  18.  */
  19. #define AppendChar(result, c) (result[++result[0]] = (c))
  20. void                        AppendUnsigned(
  21.         StringPtr                result,
  22.         unsigned long            value
  23.     );
  24. void                        AppendSigned(
  25.         StringPtr                result,
  26.         signed long                value
  27.     );
  28. void                        AppendUnsignedLeadingZeros(
  29.         StringPtr                result,
  30.         unsigned long            value,
  31.         short                    fieldWidth,
  32.         short                    terminatorChar
  33.     );
  34. void                        AppendUnsignedInField(
  35.         StringPtr                result,
  36.         unsigned long            value,
  37.         short                    fieldWidth
  38.     );
  39. void                        AppendHexLeadingZeros(
  40.         StringPtr                result,
  41.         unsigned long            value,
  42.         short                    fieldWidth
  43.     );
  44. void                        AppendBytes(
  45.         StringPtr                result,
  46.         const Ptr                source,
  47.         unsigned short            length
  48.     );
  49. void                        AppendPascalString(
  50.         StringPtr                result,
  51.         const StringPtr            value
  52.     );
  53. void                        AppendCString(
  54.         StringPtr                result,
  55.         const char                *value,
  56.         unsigned short            maxLength
  57.     );
  58. void                        AppendOSType(
  59.         StringPtr                result,
  60.         OSType                    value
  61.     );
  62. void                        AppendDouble(
  63.         StringPtr                result,
  64.         double                    value,
  65.         unsigned short            decimalPlaces
  66.     );
  67. void                        pstrcpy(
  68.         StringPtr                destination,
  69.         ConstStr255Param        source
  70.     );
  71. void                        pstrcat(
  72.         StringPtr                destination,
  73.         ConstStr255Param        source
  74.     );
  75. void                        ClearMemory(
  76.         Ptr                        dataPtr,
  77.         unsigned long            dataSize
  78.     );
  79.  
  80. /*
  81.  * AppendUnsignedLeadingZeros
  82.  * Output an n-digit decimal value with leading zeros.
  83.  */
  84. void
  85. AppendUnsignedLeadingZeros(
  86.         StringPtr            result,
  87.         unsigned long        value,
  88.         short                digits,
  89.         short                terminator
  90.         
  91.     )
  92. {
  93.         if (--digits > 0)
  94.             AppendUnsignedLeadingZeros(result, value / 10, digits, NUL);
  95.         AppendChar(result, (value % 10) + '0');
  96.         if (terminator != NUL)
  97.             AppendChar(result, terminator);
  98. }
  99.  
  100. /*
  101.  * AppendSigned
  102.  * Output a signed decimal longword.
  103.  */
  104. void
  105. AppendSigned(
  106.         StringPtr            result,
  107.         signed long            value
  108.     )
  109. {
  110.         if (value < 0) {
  111.             AppendChar(result, '-');
  112.             value = (-value);
  113.         }
  114.         AppendUnsigned(result, (unsigned long) value);
  115. }
  116.  
  117. /*
  118.  * AppendUnsigned
  119.  * Output an unsigned decimal longword.
  120.  */
  121. void
  122. AppendUnsigned(
  123.         StringPtr            result,
  124.         unsigned long        value
  125.     )
  126. {
  127.         if (value >= 10)
  128.             AppendUnsigned(result, value / 10);
  129.         AppendChar(result, (value % 10) + '0');
  130. }
  131.  
  132. /*
  133.  * Store a number right-justified in a fixed-width field.
  134.  */
  135. void
  136. AppendUnsignedInField(
  137.         StringPtr            result,
  138.         unsigned long        value,
  139.         short                fieldWidth
  140.     )
  141. {
  142.         Str255                work;
  143.         
  144.         work[0] = 0;
  145.         AppendUnsigned(work, value);
  146.         result[0] = 0;
  147.         while (work[0] < fieldWidth) {
  148.             AppendChar(result, ' ');
  149.             --fieldWidth;
  150.         }
  151.         AppendPascalString(result, work);
  152. }
  153.         
  154. /*
  155.  * AppendHexLeadingZeros
  156.  * Output a string of hex digits with leading zeros. May not
  157.  * move memory. Note that "digits" is the field width, not
  158.  * the number of hex bytes.
  159.  */
  160. void
  161. AppendHexLeadingZeros(
  162.         StringPtr            result,
  163.         unsigned long        value,
  164.         short                fieldWidth
  165.     )
  166. {
  167.         if (--fieldWidth > 0)
  168.             AppendHexLeadingZeros(result, value >> 4, fieldWidth);
  169.         value &= 0x0F;
  170.         AppendChar(result,
  171.                 (value < 10)
  172.                 ? value + '0'
  173.                 : (value + ('A' - 10))
  174.             );
  175. }
  176.  
  177. /*
  178.  * AppendOSType
  179.  * Output a 4-byte character string. Unknown bytes (characters
  180.  * below ' ') are replaced by '.'.
  181.  */
  182. void
  183. AppendOSType(
  184.         StringPtr                result,
  185.         OSType                    datum
  186.     )
  187. {
  188.         char                    value[sizeof (OSType)];
  189.         register short            i;
  190.         register unsigned char    c;
  191.         
  192.         BlockMove(&datum, value, sizeof (OSType));
  193.         AppendChar(result, '\'');
  194.         for (i = 0; i < sizeof (OSType); i++) {
  195.             c = value[i];
  196.             if (c < ' ')
  197.                 c = '.';
  198.             AppendChar(result, c);
  199.         }
  200.         AppendChar(result, '\'');
  201. }
  202.  
  203. void                        AppendDouble(
  204.         StringPtr                result,
  205.         double                    value,
  206.         unsigned short            decimalPlaces
  207.     )
  208. {
  209.         char                    tempValue[256];
  210.  
  211.         /*
  212.          * There are some things man was not meant to know.
  213.          */        
  214.         sprintf(tempValue, "%.*f", decimalPlaces, value);
  215.         AppendCString(result, tempValue, 255);
  216. }
  217.  
  218. void
  219. AppendCString(
  220.         StringPtr                result,
  221.         const char                *source,
  222.         unsigned short            maxLength
  223.     )
  224. {
  225.         register unsigned char    *ptr;
  226.         
  227.         ptr = (unsigned char *) source;
  228.         while (*ptr != '\0'
  229.             && (maxLength == 0 || (((char *) ptr) - source) < maxLength))
  230.             AppendChar(result, *ptr++);
  231. }
  232.  
  233. /*
  234.  * Append a fixed-sized string.
  235.  */
  236. void
  237. AppendBytes(
  238.         StringPtr                result,
  239.         const Ptr                source,
  240.         unsigned short            length
  241.     )
  242. {
  243.         register unsigned char    *ptr;
  244.         
  245.         ptr = (unsigned char *) source;
  246.         while (length-- > 0)
  247.             AppendChar(result, *ptr++);
  248. }
  249.  
  250. /*
  251.  * AppendPascalString (just pstrcat with a different name).
  252.  */
  253. void
  254. AppendPascalString(
  255.         StringPtr                result,
  256.         const StringPtr            datum
  257.     )
  258. {
  259.         pstrcat(result, datum);
  260. }
  261.  
  262. void
  263. pstrcpy(
  264.         StringPtr                        destination,
  265.         ConstStr255Param                source
  266.     )
  267. {
  268.         BlockMove(source, destination, source[0] + 1);
  269. }
  270.  
  271. void
  272. pstrcat(
  273.         StringPtr                        destination,
  274.         ConstStr255Param                source
  275.     )
  276. {
  277.         short                            length;
  278.         
  279.         length = 255 - destination[0];
  280.         if (length > source[0])
  281.             length = source[0];
  282.         BlockMove(&source[1], &destination[1] + destination[0], length);
  283.         destination[0] += length;
  284. }
  285.  
  286. void
  287. ClearMemory(
  288.         register Ptr                    dataPtr,
  289.         register unsigned long            dataSize
  290.     )
  291. {
  292.         while (dataSize > 0) {
  293.             *dataPtr++ = 0;
  294.             --dataSize;
  295.         }
  296. }
  297.  
  298.